package kgame5.pbe.atlantic.comp {
	import flash.display.Sprite;
	
	import kgame5.pbe.atlantic.event.GameCommandEvent;
	import kgame5.pbe.atlantic.vo.EntityMessageVO;
	import kgame5.pbe.engine.PBE;
	import kgame5.pbe.engine.entity.EntityComponent;
	import kgame5.pbe.atlantic.vo.EntityVO;
	/**
	 * @author max
	 * 2011-2-27
	 * 简单的组件，视图，逻辑，对外接口都包含其中
	 * !!严重注意，这种简化版的entity,是没有dataComp的，也就是不会被senseWorld感知
	 */
	public class SimpleCompV2 extends EntityComponent implements IEntityCompFacade{
		
		public static const NAME:String="SimpleCompV2";
		
		protected var mc_c:Sprite;
		private var layerID:int;
		
		protected var entVO:EntityVO;
		
		public function SimpleCompV2(p_layerID:int,p_entVO:EntityVO=null){
			layerID=p_layerID;
			mc_c=new Sprite();
			this.entVO=p_entVO;
		}
		
		override protected function onAdd():void{
			super.onAdd();
			PBE.viewManager.addNode(mc_c, layerID);
			
			//为了防止跳动,添加到舞台的时候就要设置好位置
			if(entVO){
				mc_c.x=entVO.spcVO.posi.x;
				mc_c.y=entVO.spcVO.posi.y;
			}
		}
		
		override protected function onRemove():void{
			super.onRemove();
			PBE.viewManager.removeNode(mc_c, layerID);
		}
		
		//#对外提供，也是供子类继承用,处理游戏命令
		//这个是有用的，有些交互不一定来自引擎内部，比如有人用手点击了水族箱
		//处理发送到entity的command,很有用
		//~~用handler来处理外部命令而不是对外直接暴露命令接口，这样当gamePlay里想执行命令式，只是executeEntityCommand即可，
		//否则则需要(PBE.lookupCompByName(entityID,compName) as XXXContorller).xxxFunc(xxx);
		public function handleToEntityCommand(commandName:String,data:Object=null):void{
			
		}
		
		

		//注意这个和ControllerComp中的command是相互独立的,原因是有的entity不需要controller
		public function sendFromEntityCommand(commandName : String,data : Object = null) : void {
			owner.eventDispatcher.dispatchEvent(new GameCommandEvent(commandName, data));
		}
		
		public function handleEntityToEntityMessage(mvo:EntityMessageVO):void{
			
		}
		
		//==============实体间发消息，比如一条鱼通知另一条鱼来吃食======
		public function sendEntityToEntityMessage(msgName : String,toID : String,scope : int=1,data : *=null) : void {
			//T.p("sendEntityToEntityMessage",msgName,toID);
			if(entVO==null){
				throw new Error("entVO不能为空！");
			}
			var mvo : EntityMessageVO = new EntityMessageVO(msgName, data, entVO, scope,toID);
			PBE.senseWorld.sendEntityToEntityMessage(mvo);
			//eventDispatcher.dispatchEvent(new EntityMessageEvent(EntityMessageEvent.TYPE, mvo));
		}
	}//end of class
}
